package cn.qfys521.mc_websocket.client

import cn.qfys521.mc_websocket.client.models.Player
import java.net.URI
import java.util.concurrent.CompletableFuture
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class ManagementWsClientTest {

    @Test
    fun `notification players joined is dispatched`() = runBlocking {
        val client = ManagementWsClient(URI("ws://localhost:25585"))

        val received = CompletableFuture<Player?>()
        client.onNotification("notification:players/joined", Player.serializer()) { p ->
            received.complete(p)
        }

        val msg = """
            {"jsonrpc":"2.0","method":"notification:players/joined","params":[{"id":"uuid","name":"jeb_"}]}
        """.trimIndent()
        client.onMessage(msg)

        val player = withTimeout(1000) { received.get() }
        assertEquals("uuid", player?.id)
        assertEquals("jeb_", player?.name)
    }
}

